package com.gg.hj.service.impl;

import com.gg.common.mapper.SysMenusMapper;
import com.gg.common.mapper.SysRolesMapper;
import com.gg.common.mapper.SysUserRoleMapper;
import com.gg.common.pojo.SysMenus;
import com.gg.common.pojo.SysRoles;
import com.gg.common.pojo.SysUserRole;
import com.gg.common.pojo.SysUsers;
import com.gg.hj.search.SysRolesSearch;
import com.gg.hj.service.SysRolesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysRolesServiceImpl implements SysRolesService {
    @Resource
    private SysRolesMapper sysRolesMapper;
    @Resource
    private SysMenusMapper sysMenusMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    //TODO 查询所有角色
    public SysRolesSearch<SysRoles> queryRoleAllService(SysRolesSearch<SysRoles> sysRolesSearch) throws Exception {
        PageHelper.startPage(sysRolesSearch.getPageNum(),sysRolesSearch.getPageSize());
        PageInfo<SysRoles> pageInfo = new PageInfo<SysRoles>(this.sysRolesMapper.queryRoleAll());
        sysRolesSearch.setTotal(pageInfo.getTotal());
        sysRolesSearch.setRows(pageInfo.getList());
        return sysRolesSearch;
    }

    @Override
    //TODO 当前登入账号查询角色具有菜单
    public List<SysMenus> searchQueryUserIdMenuService(int userId) throws Exception {
        return this.sysMenusMapper.searchQueryMenuAll(userId);
    }

    @Override
    //TODO 根据角色编号查询具有的菜单 授权目标
    public SysRolesSearch<SysMenus> searchQueryByIdMenuService(int roleTagId) throws Exception {
        List<SysMenus> sysMenus = this.sysRolesMapper.searchQueryByIdMenu(roleTagId);
        StringBuffer getMenuId = new StringBuffer();
        for(SysMenus r : sysMenus) {
            getMenuId.append(",").append(r.getId()).append(",");
        };
        SysRolesSearch<SysMenus> sysRolesSearch = new SysRolesSearch<SysMenus>();
        sysRolesSearch.setUserTagIds(getMenuId.toString());
        return sysRolesSearch;
    }

    @Override
    public int AuthorizeOrRecycleMenusService(int roleId, int[] check, int[] nocheck) {
        try {
            if(nocheck!=null){//未选择的菜单删除
                //删除授权目标菜单
                this.sysRolesMapper.removeMenuId(roleId,nocheck);
            }
            if(check!=null){//添加选中的菜单
                //查询当前授权目标拥有的菜单
                List<SysMenus> sysMenus = this.sysRolesMapper.searchQueryByIdMenu(roleId);
                //把授权目标有的id存到数组里
                int[] array = new int[sysMenus.size()];
                for(int i=0;i<sysMenus.size();i++){
                    array[i] = sysMenus.get(i).getId();
                }
                //授权目标具有id
                Integer newNums1[] = Arrays.stream(array).boxed().toArray(Integer[]::new);
                //授权id
                Integer newNums2[] = Arrays.stream(check).boxed().toArray(Integer[]::new);

                List<Integer> list1 = Arrays.asList(newNums1); //将newNums1数组转成list数组
                List<Integer> list2 = new ArrayList<Integer>();//用来存放2个数组中不相同的元素
                //判断授权目标是否具有授权菜单编号  没有添加到 list2 集合中
                for (Integer t : newNums2) {
                    if (!list1.contains(t)) {
                        list2.add(t);
                    }
                }
                // List<Integer> list2 转 int[]
                int[] roleTagId = list2.stream().mapToInt(Integer::valueOf).toArray();
                //未拥有角色编号 添加角色
                if(roleTagId.length>0){
                    //添加授权目标角色
                    this.sysRolesMapper.saveMenuId(roleId,roleTagId);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return 1;//异常返回
        }
        return 0;//正常返回
    }

    @Override
    public int saveRoleService(SysRoles sysRoles){
        try{
            this.sysRolesMapper.saveRole(sysRoles);
            Session session = SecurityUtils.getSubject().getSession();
            SysUsers user = (SysUsers) session.getAttribute("user");
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(user.getId());//用户编号
            sysUserRole.setRoleId(sysRoles.getId());//角色编号
            this.sysUserRoleMapper.insert(sysUserRole);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
